package com.yzt.dynamicmodule.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yzt.zhuju.arouter.RouterDynamic;
import com.yzt.zhuju.utils.DimensUtils;
import com.yzt.dynamicmodule.view.DynamicVideoView;
import com.yzt.zhuju.widget.tuiview.TUIImageView;
import com.yzt.zhuju.widget.tuiview.TUITextView;
import com.yzt.dynamicmodule.view.DynamicImageView;
import com.yzt.dynamicmodule.R;
import com.yzt.zhuju.network.entity.dynamic.DynamicListBean;
import com.yzt.p_base.utils.DisplayUtils;
import com.yzt.plugin_imageloader.ImageLoadUtil;

import java.util.List;

import cn.jzvd.Jzvd;

/**
 * author : bob
 * date   : 2023/2/27
 * desc   : 动态的适配器
 */
public class DynamicListAdapter extends BaseQuickAdapter<DynamicListBean, BaseViewHolder> implements LoadMoreModule {

    private final String userId = "123";

    public DynamicListAdapter() {
        super(R.layout.item_dynamic_list);
    }

    @Override
    protected void convert(BaseViewHolder holder, DynamicListBean item) {

        // 用户信息
        ImageLoadUtil.loadAvatar(getContext(), item.getAvatar(), holder.getView(R.id.img_header));
        // 昵称
        holder.setText(R.id.tv_user_name, item.getNickName());
        // 认证
        holder.setGone(R.id.img_auth, item.isAuthed());
        // 会员
        holder.setGone(R.id.img_vip, item.getMembershipLevel() != 0);
        // 性别
        TUIImageView imgGender = holder.findView(R.id.img_gender);
        imgGender.setSelected(item.getGender() == 1);
        // 年龄/时间
        holder.setText(R.id.tv_time, item.getAge() + "·" + item.getCreateTime());

        // 关注
        TUIImageView follow = holder.findView(R.id.tv_follow);
        if (userId.equals(item.getUserId())) {
            holder.setVisible(R.id.tv_follow, false);
        } else {
            holder.setVisible(R.id.tv_follow, true);
            follow.setSelected(item.isFollowed());
        }

        // 动态内容
        holder.setText(R.id.tv_content, item.getContent());

        // 0-文字 1-图片 2-视频
        DynamicImageView dynamicImageView = holder.findView(R.id.dynamic_img);
        CardView cardView = holder.findView(R.id.card_video);
        dynamicImageView.setVisibility(View.GONE);
        cardView.setVisibility(View.GONE);
//        if (item.getCommunityInfo().getType() == 1) {
        dynamicImageView.setVisibility(View.VISIBLE);
        loadImage(holder, item);
//        } else if (item.getCommunityInfo().getType() == 2) {
//            cardView.setVisibility(View.VISIBLE);
//            loadVideo(holder, item);
//        } else {
//            dynamicImageView.setVisibility(View.GONE);
//            cardView.setVisibility(View.GONE);
//        }

        // 营地
        TUITextView tv_yd = holder.findView(R.id.tv_yd);
        tv_yd.setText(item.getLocation());

        // 城市
        TextView tv_city = holder.findView(R.id.tv_city);
        tv_city.setText(item.getLocation());

        // 消息数量
        holder.setText(R.id.tv_msg, String.valueOf(item.getCommentCount()));

        // 点赞数量
        TUITextView tvLike = holder.findView(R.id.tv_like);
        tvLike.setSelected(item.isLiked());
        holder.setText(R.id.tv_like, String.valueOf(item.getLikeCount()));
    }

    public void loadImage(BaseViewHolder holder, DynamicListBean item) {
        DynamicImageView dynamicImageView = holder.findView(R.id.dynamic_img);
        // 动态图片
        if (item.getPicList().isEmpty()) {
            dynamicImageView.setVisibility(View.GONE);
        } else {
            dynamicImageView.setVisibility(View.VISIBLE);
            //  dynamicImageView.setList(item.getPicList(), item.getType());
        }
    }

    /**
     * 加载视频模块
     */
    private void loadVideo(BaseViewHolder holder, DynamicListBean item) {

        CardView cardView = holder.findView(R.id.card_video);
        List<String> path = item.getPicList();

        if (path != null && path.size() > 0 && !TextUtils.isEmpty(path.get(0))) {
            LinearLayout.LayoutParams cardParams = (LinearLayout.LayoutParams) cardView.getLayoutParams();

            cardParams.width = DisplayUtils.getScreenWidth(getContext()) - DimensUtils.dp2px(getContext(), 32);
            cardParams.height = DimensUtils.dp2px(getContext(), 216);
            cardView.setLayoutParams(cardParams);

            cardView.setVisibility(View.VISIBLE);
            DynamicVideoView jzvdStd = holder.findView(R.id.videoplayer);
            if (jzvdStd != null) {
                jzvdStd.setVolume();
                jzvdStd.setOnScreenClickListener(() -> RouterDynamic.DynamicDetailActivity(item));
                jzvdStd.setUp(path.get(0), "", Jzvd.SCREEN_NORMAL);
                Glide.with(getContext())
                        .load(path.get(0))
                        .placeholder(R.mipmap.icon_placeholder)
                        .error(R.mipmap.icon_placeholder)
                        .centerCrop()
                        .into(jzvdStd.posterImageView);
            }
        } else {
            if (cardView != null) {
                cardView.setVisibility(View.GONE);
            }
        }
    }
}